<script>
    common.setMainTitle('Phân quyền truy cập hệ thống');
</script>
<form id="mainform" name='frm' method=post action="/admin/authorization/index">
    <table width = 100% id="id-form">
        <tr>
            <td width="10%" nowrap="nowrap">Chọn vai trò
            </td>
            <td >
                <select name="code" value="" onchange="changeRole(this.value)">
                <?php
                $selected = "";
                foreach($this->roles as $role){
                    if($role->CODE == $this->code){
                        $selected = "selected=''";
                    }else{
                        $selected = "";
                    }
                ?>
                <option <?=$selected?> value="<?=$role->CODE?>"><?=$role->NAME?></option>
                <?php } ?>
                </select>
            </td>
        </tr>
    </table >
    <div id="permissionContent">
        <?php 
            $navigation = '';
            $tab = "&nbsp;&nbsp;&nbsp;&nbsp;";
            foreach($this->data as $item1){
                if(isset($this->authContent[$item1['description']])){
                    $dataAuthForModule = $this->authContent[$item1['description']];
                    $arrAllow = explode(",",$dataAuthForModule['allow']);
                    $arrDenies = explode(",",$dataAuthForModule['deny']);
        ?>
        <table class="roles" id="product-table">
            <thead>
                <tr class="alternate-row">
                    <th width="40%" style="text-align: left">&nbsp;&nbsp;<font color="#004FA0">CẤP PHÉP MODULE <?=strtoupper($item1['description'])?></th>
                    <th style="padding:0px 0px 10px 10px"></font><input type="checkbox" onclick="SelectAllBox(this,'CB_<?=strtoupper($item1['description'])?>')" name="CBALL_<?=strtoupper($item1['description'])?>" value="" title="Chọn tất cả" /></th>
                </tr>
            </thead>
            <tfoot>
            </tfoot>
            <tbody>
                <?php
                    if($item1['description'] !=""){
                        $keys = array_keys($item1['controller']);
                    }
                    for($i=0;$i<count($keys);$i++)
                    {
                        $valueChecked = "1";
                        $checked = "checked = 'true'";
                        if($dataAuthForModule['deny'] != ""){
                                $deny = $this->code.":".$keys[$i];
                                if(!in_array($deny,$arrDenies)){
                                    if(!in_array($this->code,$arrAllow)){
                                        $checked = "";
                                        $valueChecked = "0";
                                    }
                                }else{
                                    $checked = "";
                                    $valueChecked = "0";
                                }
                        }else{
                            if(!in_array($this->code,$arrAllow)){
                                $checked = "";
                                $valueChecked = "0";
                            }
                        }
                        $description = $item1['controller'][$keys[$i]]['description'];
                        $class = "";
                        if($i == (count($keys)-1)){
                            $class = "roles_bottom";
                        }
                        $hiddenValue = strtoupper($item1['description']).'_'.strtoupper($keys[$i]);
                ?>
                <tr>
                    <td class="roles_left <?=$class?>"><?=$description?>
                    </td>
                    <input type="hidden" name="<?=strtoupper($item1['description'])?>[<?=$i?>][NAME]" value="<?=strtolower($keys[$i])?>" />
                    <input type="hidden" name="<?=strtoupper($item1['description'])?>[<?=$i?>][VALUE]" value="<?=$valueChecked?>" id="<?=$hiddenValue?>" /> 
                    <td class="roles_right <?=$class?>">
                        <input type="checkbox" name="CB_<?=strtoupper($item1['description'])?>[]" <?=$checked?> onchange="changeCBValue('<?=$hiddenValue?>')" id="CB-<?=$hiddenValue?>" />
                    </td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
        <?php
        }else{
            echo "<h3><font color='red'>Không thể truy xuất được dữ liệu phân quyền cho module ",'<b>',strtoupper($item1['description']).'</b></font></h3>';
        }
        }
        ?>
    </div>
</form>
<script>
    function save(){
        document.frm.action  = "/admin/authorization/save";
        document.frm.submit();
    }
    function SelectAllBox(selobj,childobj){
         var arr = document.getElementsByName(childobj+"[]");
         for(var i=0;i<arr.length;i++){
            arr[i].checked = selobj.checked;
            var idCB = arr[i].id;
            idHidden = idCB.split("-");
            var box = document.getElementById(idHidden[1]);
            var value = box.value;
            var valueCheckAll = "0";
            if(selobj.checked === true ){
                valueCheckAll = "1"; 
            }
            if(valueCheckAll != value){
                if(value == '0'){
                    box.value = '1';
                }else{
                    box.value = '0';
                }
            }
         }
    }
    function changeCBValue(id){
        var box = document.getElementById(id);
        var value = box.value;
        if(value == '0'){
            box.value = '1';
        }else{
            box.value = '0';
        }
    }
    function changeRole(role){
        common.submitForm(document.frm,'');
    }
</script>
